package com.lz.util.log;

import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import com.lz.util.ConstantConfig;

public class LoggerUtil {
	
	private static boolean append=true;
	
	private static final FileHandler handler = getFileHandler();
	
	private LoggerUtil(){
		
	}

	private static FileHandler getFileHandler() {
		FileHandler handler=null;
		try {
			
			    String pattern = ConstantConfig.LOG_FILE;
		        int limit = 1000000; // 1 Mb
		        int numLogFiles = 3;
		        
			handler=new FileHandler(pattern, limit, numLogFiles,append);
			handler.setFormatter(new SimpleFormatter());
		} catch (Exception e) {
			System.out.println("Log Handler cannot be instantiated");
			e.printStackTrace();
		}
		return handler;
	}

	//This is the exposed method
	public static Logger getLogger(String fqcn){
		Logger logger=null;
	 try {
	        logger = Logger.getLogger(fqcn);
	        logger.addHandler(handler);
	        logger.setLevel(Level.FINE);
	    } catch (Exception e) {
	    	System.out.println("Logger cannot be instantiated");
			e.printStackTrace();
	    }
	    return logger;
	}
}
